为了账号安全,请及时绑定邮箱和手机立即绑定

Struts2 框架学习笔记之Hello World

标签:
Java

从听说Struts2这个框架到现在已经将近很多年了,但是由于工作中没有用到Struts框架,所以导致本人对Struts2这个框架是学了又忘忘了又学,而且面试跳槽Struts2框架又是必不可少的内容,于是打算从Hello World开始记录Struts2的使用以来加深对Struts2框架的印象。

  • 环境搭建
    打开Eclipse新建动态的WEB工程,将Struts2 所需Jar包加入到lib文件夹下,并在Web.xml中配置Struts2的核心过滤器StrutsPrepareAndExecuteFilter,在src的目录下新建struts.xml文件。src中新建一个class命名为HelloWorldAction 并继承自ActionSupport类。项目搭建完毕后如下截图所示:
    图片描述

  • 编写代码
    在HelloAction中编写代码如下:
package com.imooc.chapter01;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorldAction extends ActionSupport {

    /**
     * 
     */
    private static final long serialVersionUID = -7962048394325449633L;

    @Override
    public String execute() throws Exception {
        // TODO Auto-generated method stub
        return SUCCESS;
    }

}

在chapter01文件夹下文件index.jsp和success.jsp,其中index.jsp是项目访问的首页,success是index.jsp点击超链接跳转的成功页面。
index.jsp和success.jsp页面代码

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>首页</title>
</head>
<body>
<a>welcome to Struts2</a>
<a href="${pageContext.request.contextPath}/helloWolrd.action">hello World</a>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>成功页面</title>
</head>
<body>
<a>welcome to learn how to use Struts2!</a>

</body>
</html>

项目首页和结果页开发完毕后,配置struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>
<package name="hello" namespace="/" extends="struts-default">
    <action name="helloWolrd" class="com.imooc.chapter01.HelloWorldAction">
            <result name="success">/chapter_01/success.jsp</result>
        </action>
    </package>
</struts>

至此,Struts2的hello world项目开发完毕,启用Tomcat。访问项目首页效果如下图所示:
图片描述

点击超链接后:
图片描述

关于Struts2的Hello World就记录这么多,后续继续记录。。。

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
JAVA开发工程师
手记
粉丝
25
获赞与收藏
75

关注作者,订阅最新文章

阅读免费教程

  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消